<?php
/** Member queue Model
*
* This is queue class model to access DB and add, delete, reorder the queue of the member
*
* @author  webMAPZ
* @version 39
* @copyright copyright (c) 2011, Boston Univeristy Under BSD
* @todo add function model to redorder the priority list of the customer
*/
class Model_cart extends CI_Model {
    
    
     function delete_cart($cus_id , $order_id)
    {
           
           // the DB query to get the the priorities of the movies queue to reorder the next priority value for the  movie
            $query_str = "SELECT prod_id FROM `T_CUS_CART` WHERE order_id = ? AND order_purch_flag = 0";
            // run the query with the customer id
            $result = $this->db->query($query_str,$order_id);
            // get the number of rows to decrement all prioties values after the deleted movie
            $prod_id = $result->row(0)->prod_id;
            // delete query to the DB 
            $query_str = "DELETE FROM `T_CUS_CART` WHERE order_id = ? AND order_purch_flag = 0";
            // to execute the query for deleting a movie from the queue
            $result1 = $this->db->query($query_str,$order_id);
            // for loop to decrement the priorities for the rest of the movies
          
            $query_str = "UPDATE `T_PRODUCTS` SET IN_STOCK = IN_STOCK + 1 WHERE prod_id = ?";                
           // run the query to update the priority for the other movies
            $result2 = $this->db->query($query_str,$prod_id);
           
           if($result1 && $result2)
            {           
                return true;
            }
            else
            {
                return false;
            }
    }
    
    
    function delete_all_cart($cus_id)
    {
          
            $query_str = "SELECT prod_id FROM `T_CUS_CART` WHERE cus_id = ? AND order_purch_flag = 0";
            $result = $this->db->query($query_str,$cus_id);
            
            $res = 
            $result->result_array();
           foreach ($res as $item) 
              { 
                $query_str = "UPDATE `T_PRODUCTS` SET IN_STOCK = IN_STOCK + 1 WHERE prod_id = ?";                
               // run the query to update the priority for the other movies
                $result2 = $this->db->query($query_str,$item['prod_id']);
                }
            
            // delete query to the DB 
            if($result2){
                $query_str = "DELETE FROM `T_CUS_CART` WHERE cus_id = ? AND order_purch_flag = 0";
                    // to execute the query for deleting a movie from the queue
                    $result1 = $this->db->query($query_str,$cus_id);
            }
            
           if($result2 && $result1)
            {           
                return true;
            }
            else
            {
                return false;
            }
    }
    function get_cart($cus_id)
    {
            // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT order_id ,CC.cus_id , CC.prod_id, CC.store_loc_id, CC.special_flag, CC.special_desc, CC.order_date,
               CC.order_purch_flag, CC.status , CC.shipped_on, CC.shipping_notes, CC.shipping_method ,P.p_name , 
               S.store_address, M.m_type, P.p_description ,
               P.p_price , P.p_sale_price , P.p_tax , P.weight , P.p_images , P.p_type , P.options
            FROM `T_CUS_CART` CC,`T_CUSTOMERS`C,`T_DELIVERY_LOCATION` S,`T_PRODUCTS` P ,`T_Menu` M 
            WHERE CC.cus_id = C.cus_id and CC.prod_id = P.prod_id and CC.store_loc_id = S.store_loc_id and
            CC.order_purch_flag = 0 and CC.special_flag is null and CC.shipped_on is null and P.menu_id = M.menu_id and 
            shippable = 1  and CC.cus_id = ? ORDER BY CC.order_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str, $cus_id);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result;
            }
            else
            {
                return FALSE;
            }
    }
    
    
   function update_order_special($order_id , $email ,$special_desc)
    {
            // The DB query to search for the movies queue of a specific user with a specific user id
            
            $query_str = "SELECT p_name FROM `T_CUS_CART` CC, `T_PRODUCTS` P
            WHERE CC.prod_id = P.prod_id and order_id = ?";
       
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str,  $order_id );
            
            $p_name = $result->row(0)->p_name;
            
            $query_str2 = "UPDATE `T_CUS_CART` SET special_flag= 1, special_desc = ?
            WHERE order_id = ? ";
       
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result1 = $this->db->query($query_str2, ARRAY ($special_desc , $order_id ));
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result && $result1)
            {
               
                $this->email->from('yogettenet@gmail.com', 'YOGETTE\'s Team');
                $this->email->to($email); 
                $this->email->cc('zs.master@hotmail.com'); 
                $this->email->subject('Special Items Confirmation from YOGETTE');
                $this->email->message('This is a Confirmation, You has Requested Special Item/s Please Allow 24 hours for reviewing. Item/s will be Processes Once Approved, Our Employees Will contact You Shortly, Thank you and Hope you Enjoy your Shopping. 
                    your Special order is('.$p_name.').('.$special_desc.') Thank you and Have a nice day.');	
                $this->email->send();
                return TRUE;
            }
            else
            {
                return FALSE;
            }
    }
    
    
    function emp_update_order_special($order_id , $email ,$special_desc)
    {
            // The DB query to search for the movies queue of a specific user with a specific user id
            
            $query_str = "SELECT p_name FROM `T_CUS_CART` CC, `T_PRODUCTS` P
            WHERE CC.prod_id = P.prod_id and order_id = ?";
       
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str,  $order_id );
            
            $p_name = $result->row(0)->p_name;
            
            $query_str2 = "UPDATE `T_CUS_CART` SET special_flag= null , special_desc = ?
            WHERE order_id = ? ";
       
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result1 = $this->db->query($query_str2, ARRAY ($special_desc , $order_id ));
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result && $result1)
            {
               
                $this->email->from('yogettenet@gmail.com', 'YOGETTE\'s Team');
                $this->email->to($email); 
                $this->email->cc('zs.master@hotmail.com'); 
                $this->email->subject('Special Items Response from YOGETTE');
                $this->email->message('This is a Response, your previous Request on Special Item/s. the item has not been Approved, Sorry for any inconvenience ,. 
                    your Special order ('.$p_name.'). has a response commented from one of our team member : ('.$special_desc.').');	
                $this->email->send();
                return TRUE;
            }
            else
            {
                return FALSE;
            }
    }
    
    
    
    
    function get_cart_item($order_id)
    {
            // The DB query to search for the item queue of a specific user with a specific order id
            $query_str = "SELECT order_id ,CC.cus_id , CC.prod_id, CC.store_loc_id, CC.special_flag, CC.special_desc, CC.order_date,
               CC.order_purch_flag, CC.status , CC.shipped_on, CC.shipping_notes, CC.shipping_method ,P.p_name , 
               S.store_address, M.m_type, P.p_description , C.cus_pay_id , 
               P.p_price , P.p_sale_price , P.p_tax , P.weight , P.p_images , P.p_type , P.options
            FROM `T_CUS_CART` CC,`T_CUSTOMERS`C,`T_DELIVERY_LOCATION` S,`T_PRODUCTS` P ,`T_Menu` M 
            WHERE CC.cus_id = C.cus_id and CC.prod_id = P.prod_id and CC.store_loc_id = S.store_loc_id and
            CC.order_purch_flag = 0 and CC.special_flag is not null and special_desc is not null and CC.shipped_on is null and P.menu_id = M.menu_id and 
            shippable = 1  and CC.order_id = ? ORDER BY CC.order_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str, $order_id);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result;
            }
            else
            {
                return FALSE;
            }
    }
    
    
}

?>